package com.dilidili.controller;

import com.alibaba.fastjson.JSON;
import com.dilidili.service.IBarrageService;
import com.domain.Mbarrage;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * 【请填写功能名称】Controller
 * 
 * @author ruoyi
 * @date 2022-03-31
 */
@RestController
@RequestMapping("/system/barrage")
public class BarrageController
{

    @Autowired
    private IBarrageService barrageService;
    @Resource
    private RedisTemplate redisTemplate;
    @RequestMapping(value = "/v3", method = RequestMethod.POST)
    public Object posttest(@RequestBody Mbarrage test){
        //添加弹幕
        System.out.println(test);
        Map map=new HashMap();
        Integer result =barrageService.add(test);
        String key = "barrage_" + test.getId();
        if(result>0){
            boolean hasKey = redisTemplate.hasKey(key);
            if (hasKey) {
                //删除缓存
                redisTemplate.delete(key);
                System.out.println("删除缓存中的key-----------> " + key);
            }
        }
        map.put("code",0);
        List lists=new ArrayList();
        List<Map<String,Object>> list1=barrageService.find(Integer.parseInt(test.getId()));
        for (Map<String, Object> list : list1) {
            List list2 = new ArrayList<>();
            list2.add(list.get("barrage_video_time"));
            list2.add(list.get("barrage_type"));
            list2.add(list.get("barrage_color"));
            list2.add(list.get("barrage_user_id"));
            list2.add(list.get("barrage_content"));
            lists.add(list2);
        }
        lists.add(list1);
        map.put("data",lists);
        redisTemplate.opsForValue().set(key, JSON.toJSON(map), 5, TimeUnit.HOURS);
        map = (Map) redisTemplate.opsForValue().get(key);
        return map;
    }
    @RequestMapping(value = "/v3", method = RequestMethod.GET)
    public Object gettest( Integer id){
        //获取该视频id下的弹幕
        Map map=new HashMap();
        String key = "barrage_" + id;
        boolean hasKey = redisTemplate.hasKey(key);
        if (hasKey) {
            map = (Map) redisTemplate.opsForValue().get(key);
        } else {
            map.put("code",0);
            List lists=new ArrayList();
            List<Map<String,Object>> list1=barrageService.find(id);
            for (Map<String, Object> list : list1) {
                List list2 = new ArrayList<>();
                list2.add(list.get("barrage_video_time"));
                list2.add(list.get("barrage_type"));
                list2.add(list.get("barrage_color"));
                list2.add(list.get("barrage_user_id"));
                list2.add(list.get("barrage_content"));
                lists.add(list2);
            }
            lists.add(list1);
            map.put("data",lists);
        }
        return map;
    }

    @RequestMapping("queryByUserId")
    public Object queryByBatch(Long userId,
                               @RequestParam(defaultValue = "0", required = false)Long videoId,
                               @RequestParam(defaultValue = "", required = false)String search,
                               @RequestParam(defaultValue = "1", required = false) Integer currentPage,
                               @RequestParam(defaultValue = "5", required = false) Integer pageSize)
    {
        PageHelper.startPage(currentPage, pageSize);
        List<Map> submission = barrageService.Submission(userId, videoId, search);
        PageInfo pageInfo = new PageInfo(submission);
        return pageInfo;
    }

    @RequestMapping("/del")
    public void del(Long id){
        redisTemplate.delete("barrage_"+id);
        barrageService.deleteBarrageByBarrageId(id);
    }
    @RequestMapping("/dels")
    public void dels(Long[] ids){
        for (Long id : ids) {
            redisTemplate.delete("barrage_"+id);
        }
        barrageService.deleteBarrageByBarrageIds(ids);
    }
}
